package com.dajie.pt.service.utils;

import com.dajie.core.profile.ProfileServicesContext;
import com.dajie.core.profile.model.CardInfo;
import com.dajie.core.profile.model.Education;
import com.dajie.core.profile.model.UserDetail;
import com.dajie.core.profile.service.CardInfoService;
import com.dajie.core.profile.service.ProfileService;
import com.dajie.infra.user.InfraUserServiceContext;
import com.dajie.infra.user.model.UserBase;
import com.dajie.infra.user.model.UserPhone;
import com.dajie.infra.user.service.UserBaseService;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 14-10-28.
 */
public class Dictionary {
    public static final ProfileService profileService = ProfileServicesContext.getInstance().getProfileService();
    public static final UserBaseService userService = InfraUserServiceContext.getInstance().getUserService();
    public static final CardInfoService cardInfoService = ProfileServicesContext.getInstance().getCardService();
    public static Pattern pattern = Pattern.compile("^1\\d{10}$");

    public enum KEYS {
        UID("UID"), UNAME("UNAME"), MOBILE("MOBILE"), SCHOOLNAME("SCHOOLNAME"), CORPNAME("CORPNAME");
        private String name;

        private KEYS(String name) {
            this.name = name;
        }

        //通过属性值获取枚举
        public static KEYS getKeyByName(String name) {
            for (KEYS key : KEYS.values()) {
                if (key.getName().equals(name)) {
                    return key;
                }
            }
            return null;
        }

        public String getName() {
            return name;
        }

    }

    public static boolean isContainAllKey(List<String> keys) {
        if (keys == null || keys.size() == 0)
            return false;
        for (String key : keys) {
            if (KEYS.getKeyByName(key) == null)
                return false;
        }
        return true;
    }

    public static Map<String, String> getInformation(Map<String, String> inputMap, List<String> keys) {
        Map<String, String> map = new HashMap();
        map.putAll(inputMap);
        String uid = inputMap.get(Constant.UID);
        Iterator<String> iterator = keys.iterator();
        while (iterator.hasNext()) {
            String keyName = iterator.next();
            KEYS keyEnum = KEYS.getKeyByName(keyName);  //转换成枚举类型
            switch (keyEnum) {
                case UID:
                    map.put(keyName, String.valueOf(uid));
                    break;
                case MOBILE:
                    UserPhone userPhone = userService.getDefaultPhoneByUid(Integer.parseInt(uid));
                    UserDetail userDetail1 = profileService.getUserDetailByUid(Integer.parseInt(uid));
                    String mobile = "";
                    if (userPhone == null) {
                        mobile = userDetail1.getMobile();
                    } else {
                        mobile = userPhone.getPhoneNumber();
                    }
                    if (mobile.contains("-")) {
                        String[] t_mobile = mobile.split("-");
                        if (t_mobile.length > 1) {
                            mobile = mobile.split("-")[1];
                        }
                    }
                    if (!pattern.matcher(mobile).find()) {
                        mobile = "";
                    }
                    map.put(keyName, mobile);
                    break;
                case SCHOOLNAME:
                    String school = null;
                    Education education = profileService.getHighestEducationByUid(Integer.parseInt(uid));
                    if (education != null) {
                        school = education.getSchoolName();
                    }
                    map.put(keyName, school);
                    break;
                case UNAME:
                    String name = null;
                    UserBase userBase = userService.getUserByUid(Integer.parseInt(uid));
                    if (userBase != null) {
                        name = userBase.getName();
                    }
                    map.put(keyName, name);
                    break;
                case CORPNAME:
                    String corpName = null;
                    CardInfo userCard = cardInfoService.getCardInfoByUid(Integer.parseInt(uid));
                    if (userCard != null) {
                        corpName = userCard.getCorpName();
                    }
                    map.put(keyName, corpName);
                    break;
            }
        }
        //查询参数不全数据舍弃
        for (String k : map.keySet()) {
            if (map.get(k) == null || "".equals(map.get(k))){
                return null;
            }

        }
        return map;
    }

}
